﻿/**
 * Copyright (C) 2011 original author and authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NGuice.Inject.Internal.Util;

namespace NGuice.Inject.Spi
{
    //完成！
    /// <summary>
    /// 在一个类型中注入静态域和方法的请求。请求将在一个module中使用
    /// <code>NGuice.Inject.Binder#RequestStaticInjection(Type[])</code>语法显式创建
    /// </summary>
    public sealed class StaticInjectionRequest : IElement
    {
        private readonly object source;
        private readonly Type type;

        public object Source
        {
            get { return source; }
        }

        public Type Type
        {
            get { return type; }
        }

        internal StaticInjectionRequest(object source, Type type)
        {
            this.source = Preconditions.CheckNotNull(source, "source");
            this.type = Preconditions.CheckNotNull(type, "type");
        }

        /// <summary>
        /// 返回完成此请求的将被注入到类型中的静态方法和域
        /// </summary>
        /// <returns>
        /// 可能为空注入点集合。集合具有一个特殊的迭代顺序。首先是所有域（属性）其次是方法。在域内，
        /// 超类型后面是子类型的域，类似的超类型的方法也在子类型方法之前
        /// </returns>
        /// <exception cref="ConfigurationException">
        /// 如果在类型中是一个异常的注入点，例如具有多个绑定注释的域，异常中的<code>ConfigurationException#GetPartialValue()</code>
        /// 将是集合中的一个有效的注入点
        /// </exception>
        public ISet<InjectionPoint> GetInjectionPoints()
        {
            return InjectionPoint.ForStaticMethodsAndFields(type);
        }

        public T AcceptVisitor<T>(IElementVisitor<T> visitor)
        {
            return visitor.Visit(this);
        }

        public void ApplyTo(IBinder binder)
        {
            binder.WithSource(Source).RequestStaticInjection(type);
        }
    }
}
